package com.wonder.sms.smsbao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wonder.admin.service.SysConfigService;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.WonderUtil;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

@Service
@Log
public class SmsBaoMessageSendService {

	@Autowired
	SysConfigService sysConfigService;

	/**
	 * send sms
	 * @param phone
	 * @param code
	 * @return
	 */
	public String sendSms(String phone, String code) {

		// temporary hard coded need to add configuration
		String username = sysConfigService.getCacheKey("smsbao_account"); //"haxi666";
		String password = sysConfigService.getCacheKey("smsbao_api_key"); //"4128063e7efa4ba3ad37eba1792361e1";
		String httpUrl =  sysConfigService.getCacheKey("smsbao_url")+"/sms";
		String areaCode = phone.substring(0,2);
		String smsContent = "【THRGAME】Your verification code is {code}， valid in 1 minute";
		smsContent = smsContent.replace("{code}",code);
		// basic validation need to improve international numbers only contains + sign
		if (!areaCode.equals("86")) {
			httpUrl = sysConfigService.getCacheKey("smsbao_url")+"/wsms";
			phone = "+" +phone;
		}else{
			phone = phone.substring(2);
		}

		StringBuffer httpArg = new StringBuffer();
		httpArg.append("u=").append(username).append("&");
		httpArg.append("p=").append(WonderUtil.Md5(password)).append("&");
		httpArg.append("m=").append(WonderUtil.encodeUrlString(phone, "UTF-8")).append("&");
		httpArg.append("c=").append(WonderUtil.encodeUrlString(smsContent, "UTF-8"));

		String result = request(httpUrl, httpArg.toString());
		if(result.equals("0")) return "ok";
		return result;

	}

	/**
	 * prepare the request 
	 * @param httpUrl
	 * @param httpArg
	 * @return
	 */
	private String request(String httpUrl, String httpArg) {
		BufferedReader reader = null;
		String result = null;
		StringBuffer sbf = new StringBuffer();
		httpUrl = httpUrl + "?" + httpArg;

		try {
			URL url = new URL(httpUrl);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");
			connection.connect();
			InputStream is = connection.getInputStream();
			reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String strRead = reader.readLine();
			if (strRead != null) {
				sbf.append(strRead);
				while ((strRead = reader.readLine()) != null) {
					sbf.append("\n");
					sbf.append(strRead);
				}
			}
			reader.close();
			result = sbf.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}



}
